package org.csu.mypetstore.controller;

import com.alibaba.fastjson.JSONObject;
import com.zhenzi.sms.ZhenziSmsClient;
import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.impl.AccountServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Controller
@RequestMapping("/code")
public class CodeController {
    @Autowired
    private AccountService accountService = new AccountServiceImpl();

    private Map<String,String> codeMap = new HashMap<>();
    @PostMapping
    @ResponseBody
    public String sendSms(String number) {
        try {
            if(codeMap.containsKey(number))
            {
                codeMap.remove(number);
            }
            JSONObject json = null;
            //生成6位验证码
            String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000);
            //发送短信
            //apiUrl不用改，appId和appSecret改成自己的即可
            ZhenziSmsClient client = new ZhenziSmsClient("https://sms_developer.zhenzikj.com","108557", "a7bad66f-18e6-481b-9553-5639f9be1f40");
            Map<String,Object> map = new HashMap<>();
            //map里number为电话号码，templateId为网站里设置的模板的编号，templateParams的每一个变量按照模板从前往后为{1}{2}..的内容
            map.put("number", number);
            map.put("templateId", "4409");
            String[] templateParams = new String[2];
            templateParams[0] = verifyCode;
            templateParams[1] = "1";
            map.put("templateParams", templateParams);
            String result = client.send(map);
            json = JSONObject.parseObject(result);
            System.out.println(json.getIntValue("code"));
            //以json存放，这里使用的是阿里的fastjson
            json = new JSONObject();
            if(json.getIntValue("code") != 0)//发送短信失败
            {
                json.put("status", "NOK");
                json.put("msg", "验证码发送失败");
                json.put("data", verifyCode);
                return json.toString();
            }
            codeMap.put(number,verifyCode);
            json.put("status","OK");
            json.put("msg","验证码发送完毕");
            json.put("data", verifyCode);
            return json.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping
    @ResponseBody
    public String checkSms(String username,String phone,String checkCode)
    {
        Account account = accountService.getAccountByUsername(username);
        System.out.println(checkCode);
        System.out.println(codeMap.get(phone));
        JSONObject jsonObject = new JSONObject();
        if(!account.getPhone().equals(phone))
        {
            jsonObject.put("status","NOK");
            jsonObject.put("msg","账号与手机号未绑定!");
            jsonObject.put("data",null);
            return jsonObject.toString();
        }
        else if(!codeMap.get(phone).equals(checkCode))
        {
            jsonObject.put("status","NOK");
            jsonObject.put("msg","验证码错误!");
            jsonObject.put("data",null);
            return jsonObject.toString();
        }
        else{
            codeMap.remove(phone);
            jsonObject.put("status","OK");
            jsonObject.put("msg","登录成功!");
            jsonObject.put("data",account);
            return jsonObject.toString();
        }
    }
}